package com.baidu.customview.widget.shader;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.SweepGradient;
import android.graphics.Xfermode;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.baidu.customview.R;
import com.baidu.customview.utils.MetricsUtils;

/**
 * Created by imagine on 2015/11/14.
 */
public class ShaderView extends View {
    private final int RECT_SIZE = 400;//矩形尺寸
    private Paint p;
    private int left, top, right, bottom;
    private DisplayMetrics metrics;


    public ShaderView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        metrics = MetricsUtils.winMetrics(context);
        int screenX = metrics.widthPixels / 2;
        int screenY = metrics.heightPixels / 2;
        left = screenX - RECT_SIZE;
        top = screenY - RECT_SIZE;
        right = screenX + RECT_SIZE;
        bottom = screenY + RECT_SIZE;
        p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        //shader first in y direct
        //set shader
        p.setShader(getBitmapShader());
        //p.setShader(getLinearGradient());
        //p.setShader(getSweepGradient());
        //p.setShader(getRadiaGradient());
        //p.setShader(getComposeShader());
    }

    //bitmap shader
    @NonNull
    private BitmapShader getBitmapShader() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.peien_face);
        BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
//        p.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.REPEAT));
//        p.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        Matrix matrix = new Matrix();
        matrix.setTranslate(metrics.widthPixels / 4, metrics.widthPixels / 5);
        matrix.setRotate(5);
//        matrix.setValues(new float[]{});
        shader.setLocalMatrix(matrix);
        return shader;
    }

    //linear gradient
    private LinearGradient getLinearGradient() {
        return new LinearGradient(0, 0, metrics.widthPixels, metrics.heightPixels, Color.GREEN, Color.WHITE, TileMode.CLAMP);
    }

    //sweep gradient
    private SweepGradient getSweepGradient() {
        return new SweepGradient(metrics.widthPixels / 2, metrics.heightPixels / 2, new int[]{Color.GREEN, Color.WHITE}, null);
    }

    //radial gradient
    private RadialGradient getRadiaGradient() {
        return new RadialGradient(metrics.widthPixels / 2, metrics.heightPixels / 2, metrics.widthPixels / 2, new int[]{Color.GREEN, Color.WHITE}, null, TileMode.CLAMP);
    }

    //compose shader
    private ComposeShader getComposeShader() {
        return new ComposeShader(getBitmapShader(), getLinearGradient(), new Xfermode());
    }

    public ShaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public ShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ShaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0, 0, metrics.widthPixels, metrics.heightPixels, p);
    }
}
